package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StateCode;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.dto.WeiboInsertParam;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.vo.UserSelectQuery;
import cn.tedu._04weibo.pojo.vo.WeiboDetailQuery;
import cn.tedu._04weibo.pojo.vo.WeiboIndexQuery;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.ConstraintViolationException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Validated
@RestController
@Api(tags = "02.微博管理")
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @ApiOperation("发布微博")
    @ApiOperationSupport(order = 10)
    @PostMapping("list")
    public JsonResult insert(@RequestBody WeiboInsertParam weiboInsertParam, @ApiIgnore HttpSession session){
        UserSelectQuery userSelectQuery=(UserSelectQuery) session.getAttribute("user");
//        if (userSelectQuery==null){
//            return new JsonResult(StateCode.NOT_LOGIN);
//        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboInsertParam,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userSelectQuery.getId());
        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }
    //查看微博列表
    @ApiOperation("微博列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("selectIndex")
    public JsonResult list(){
//        UserSelectQuery query = (UserSelectQuery) session.getAttribute("user");
//        if (query==null){
//            return new JsonResult(StateCode.NOT_LOGIN);
//        }
//        List<WeiboIndexQuery> weiboIndexQuery =weiboMapper.weiboList();
//        return JsonResult.ok(weiboIndexQuery);

        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable serializable = ops.get("Weibo_List");//这个名字随便取， 要和set里的名字一致
        //用Redis来进行数据的查询
        if (serializable!=null){
            //查看Redis内存中是否有数据
            log.debug("数据来自Redis");
            return JsonResult.ok(serializable);
        }
        //如果Redis缓存中没有数据，则需要在MySQL里面去查询
        log.debug("数据来源MySQL");
        List<WeiboIndexQuery> weiboIndexQuery =weiboMapper.weiboList();
        ops.set("Weibo_List", (Serializable) weiboIndexQuery,30, TimeUnit.SECONDS);
        return JsonResult.ok(weiboIndexQuery);
    }
    @ApiOperation("微博详情页")
    @ApiOperationSupport(order = 30)
    @GetMapping("selectById")
    public JsonResult selectById(@Range(min = 100,max = 500,message = "微博的编号id必须在100-500之间") @RequestParam Integer  id){
//        if (id<0){
//            throw new ConstraintViolationException("微博的编号id必须在100-500之间");
//        }
        log.debug("id"+id);
        List<WeiboDetailQuery > weiboDetailQuery  =weiboMapper.WeiboDetail(id);
        return JsonResult.ok(weiboDetailQuery);
    }
}
